<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ obj.title }}</span>
          <!-- 单图 -->
          <img class="thumb" v-if="obj.cover.type === 1" :src="obj.cover.images[0]" />
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="obj.cover.type === 3">
          <img class="thumb" v-for="(item, index) in obj.cover.images" :key="index" :src="item" />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ obj.aut_name }}</span>
            <span>{{ obj.comm_count }}评论</span>
            <span>{{ formatTime(obj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="crossBTN" v-if="isShow" />
        </div>
      </template>
    </van-cell>

    <!-- 反馈面板 -->
    <van-action-sheet
      :cancel-text="cancelText"
      @cancel="onCancel"
      v-model="show"
      :actions="actions"
      @select="onSelect"
      get-container="body"
      @click-overlay="overlayBTN"
    />
  </div>
</template>

<script>
import { formatTime } from '@/utils/date'
import { firstActions, secondActions } from '@/api/report'
import { dislikesAPI, reportsAPI } from '@/api'
export default {
  name: 'ArtItem',
  data () {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  props: {
    obj: {
      type: Object
    },
    isShow: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    formatTime (str) {
      return formatTime(str)
    },
    // 反馈按钮
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      console.log(item)
      if (item.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        await dislikesAPI(this.obj.art_id)
        this.$Notify({ type: 'success', message: '反馈成功' })
        this.show = false
      } else {
        const dataObj = {
          artID: this.obj.art_id,
          type: item.value
        }
        console.log(dataObj)
        if (item.value === 0) {
          dataObj.remark = '小可爱哈哈哈'
        }
        await reportsAPI(dataObj)
        this.$Notify({ type: 'success', message: '举报成功' })
        this.show = false
      }
    },
    crossBTN () {
      this.show = true
    },
    // 取消事件
    onCancel () {
      this.show = true
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
      } else {
        this.show = false
      }
    },
    // 遮罩层事件
    overlayBTN () {
      this.actions = firstActions
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
